package com.ctvit.cch.core.service.impl;

import java.time.LocalDateTime;
import java.util.Objects;
import java.util.function.Consumer;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.ctvit.cch.core.enums.AuditStatusEnum;
import com.ctvit.cch.core.enums.AuditTypeEnum;
import com.ctvit.cch.core.mapper.CreativeAuditMapper;
import com.ctvit.cch.core.mapper.CreativeTopicMapper;
import com.ctvit.cch.core.pojo.domain.CreativeAudit;
import com.ctvit.cch.core.pojo.domain.CreativeTopic;
import com.ctvit.cch.core.pojo.dto.creative.CreativeAuditApproveDTO;
import com.ctvit.cch.core.pojo.dto.creative.CreativeAuditRejectDTO;
import com.ctvit.cch.core.service.ICreativeAuditService;
import com.ruoyi.common.utils.SecurityUtils;

/**
 * 选题审批Service业务层处理
 *
 * @author heyingcheng
 * @date 2025/10/11
 */
@Service
public class CreativeAuditServiceImpl extends ServiceImpl<CreativeAuditMapper, CreativeAudit>
        implements ICreativeAuditService {

    private final CreativeTopicMapper creativeTopicMapper;

    public CreativeAuditServiceImpl(CreativeTopicMapper creativeTopicMapper) {
        this.creativeTopicMapper = creativeTopicMapper;
    }

    private boolean execute(String topicId, Integer auditStatus, String auditMessage,
            Consumer<LambdaUpdateWrapper<CreativeTopic>> consumer) {
        CreativeTopic topic = creativeTopicMapper.selectById(topicId);

        CreativeAudit audit = new CreativeAudit();
        BeanUtils.copyProperties(Objects.requireNonNull(topic), audit);

        audit.setAuditType(AuditTypeEnum.TOPIC.getCode());
        audit.setAuditStatus(auditStatus);
        audit.setAuditTime(LocalDateTime.now());
        audit.setAuditMessage(StringUtils.defaultString(auditMessage));
        audit.setAuditorId(SecurityUtils.getUserId());

        if (save(audit)) {
            LambdaUpdateWrapper<CreativeTopic> wrapper = new LambdaUpdateWrapper<>();
            wrapper.eq(CreativeTopic::getTopicId, topic.getTopicId());
            wrapper.set(CreativeTopic::getAuditorId, audit.getAuditorId());
            wrapper.set(CreativeTopic::getAuditStatus, auditStatus);
            wrapper.set(CreativeTopic::getAuditTime, audit.getAuditTime());
            wrapper.set(CreativeTopic::getAuditMessage, audit.getAuditMessage());

            consumer.accept(wrapper);

            return SqlHelper.retBool(creativeTopicMapper.update(wrapper));
        }

        return false;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean approve(CreativeAuditApproveDTO dto) {
        return execute(dto.getTopicId(), AuditStatusEnum.APPROVED.getCode(), dto.getAuditMessage(), wrapper -> {
            if (StringUtils.isNotBlank(dto.getTitle())) {
                wrapper.set(CreativeTopic::getTitle, dto.getTitle());
            }

            if (StringUtils.isNotBlank(dto.getContent())) {
                wrapper.set(CreativeTopic::getContent, dto.getContent());
            }
        });
    }

    @Override
    public boolean reject(CreativeAuditRejectDTO dto) {
        return execute(dto.getTopicId(), AuditStatusEnum.REJECTED.getCode(), dto.getAuditMessage(), wrapper -> {
        });
    }

}
